package com.bit.demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2021-12-04
 * Time: 16:03
 */
// 动物类
class Animal {
    protected String name;
    protected int age;

    public Animal (String name,int age) {
        this.name = name;
        this.age = age;
    }
    public void eat (String food) {
        System.out.println(name+"正在eat "+food+"age: "+age);
    }

}
// 用Dog 继承Animal
class Dog extends Animal {
    public Dog (String name,int age){
        super(name,age); // 使用super调用父类的构造方法
    }
    @Override  // 重写
    public void eat (String food) {
        System.out.println(name+"正在eat "+food+"age: "+age);
    }
}
public class Test2 {
    public static void main(String[] args) {
        Dog dog = new Dog("haha",21);
        Animal dog1 = dog;   // 向上转型：将子类的对象赋值给父类的引用
        dog1.eat("谷子");
    }

}
